<template>
  <div class="app-container">
    <div class="search-bar">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="用户名">
          <el-input
            v-model="queryParams.username"
            placeholder="请输入用户名"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="手机号">
          <el-input
            v-model="queryParams.mobile"
            placeholder="请输入手机号"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <template #icon><Search /></template>
            搜索
          </el-button>
          <el-button @click="handleResetQuery">
            <template #icon><Refresh /></template>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
      <el-table
          ref="dataTableRef"
          v-loading="loading"
          :data="pageData"
          highlight-current-row
          border
      >
        <el-table-column key="id" label="ID" prop="id" min-width="80" align="center" />
        <el-table-column key="orderNo" label="订单号" prop="orderNo" min-width="180" align="center" />
        <el-table-column key="userId" label="用户ID" prop="userId" min-width="100" align="center" />
        <el-table-column key="nickname" label="用户昵称" prop="nickname" min-width="100" align="center" />
        <el-table-column key="totalMoney" label="金额" prop="totalMoney" min-width="100" align="center">
          <template #default="scope">
            {{ scope.row.totalMoney ? `¥${scope.row.totalMoney}` : '-' }}
          </template>
        </el-table-column>
        <el-table-column key="totalAmount" label="数量" prop="totalAmount" min-width="100" align="center" />
        <el-table-column key="paymentMethod" label="支付类型" prop="paymentMethod" min-width="120" align="center">
          <template #default="scope">
            {{ getPaymentMethodText(scope.row.paymentMethod, scope.row.offPayChannel) }}
          </template>
        </el-table-column>
        <el-table-column key="leadUserId" label="上级ID" prop="leadUserId" min-width="100" align="center" />
        <el-table-column key="createTime" label="创建时间" prop="createTime" min-width="160" align="center" />
        <el-table-column key="remark" label="备注" prop="remark" min-width="150" align="center" />
        <el-table-column key="voucherUrl" label="支付凭证" prop="voucherUrl" min-width="120" align="center">
          <template #default="scope">
            <el-image v-if="scope.row.voucherUrl" :src="scope.row.voucherUrl" :preview-src-list="[scope.row.voucherUrl]" style="width:40px;height:40px;object-fit:cover" fit="cover" :preview-teleported="true" :initial-index="0" />
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column key="auditStatus" label="审核状态" prop="auditStatus" min-width="100" align="center">
          <template #default="scope">
            <el-tag :type="getAuditStatusType(scope.row.auditStatus)">
              {{ getAuditStatusText(scope.row.auditStatus) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column key="auditAction" label="操作" min-width="120" align="center">
          <template #default="scope">
            <el-button v-hasPerm="['member:member-money-order:doAudit']" v-if="scope.row.auditStatus === 0" size="small" type="primary" @click="openAuditDialog(scope.row)">审核</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
          v-if="total > 0"
          v-model:total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="handleQuery()"
      />
    </el-card>

    <el-dialog v-model="auditDialog.visible" title="充值审核" width="400px">
      <div v-if="auditDialog.row">
        <div style="text-align:center;margin-bottom:16px;">
          <el-image :src="auditDialog.row.voucherUrl" style="max-width:300px;max-height:300px;" fit="contain" v-if="auditDialog.row.voucherUrl" :preview-src-list="[auditDialog.row.voucherUrl]" :preview-teleported="true" :initial-index="0" />
          <div v-else>无支付凭证</div>
        </div>
        <el-form label-width="60px">
          <el-form-item label="备注">
            <el-input v-model="auditDialog.remark" type="textarea" placeholder="请输入审核备注" />
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button @click="auditDialog.visible = false">取消</el-button>
        <el-button type="success" @click="submitAudit(1)" :loading="auditDialog.loading">审核通过</el-button>
        <el-button type="danger" @click="submitAudit(2)" :loading="auditDialog.loading">审核拒绝</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import RechargeAPI from "@/api/genealogy/recharge.api";
import { ref, reactive, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import { Search, Refresh } from '@element-plus/icons-vue';
import { useUserStore } from "@/store";
const { areaCodeBelong, areaCodeScope } = useUserStore().userInfo;
const queryFormRef = ref();
const loading = ref(false);
const total = ref(0);

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  username: '',
  mobile: '',
});

const pageData = ref([]);

const auditDialog = reactive({
  visible: false,
  row: null as any,
  remark: '',
  loading: false,
});

function handleQuery() {
  loading.value = true;
  RechargeAPI.getBuyPage(queryParams)
    .then((data: any) => {
      pageData.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

function handleResetQuery() {
  queryFormRef.value?.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

function getPaymentMethodText(method: string, offPayChannel?: string | number) {
  if (method === 'offline') {
    switch (offPayChannel) {
      case '0':
      case 0:
        return '线下微信支付';
      case '1':
      case 1:
        return '线下支付宝支付';
      default:
        return '线下充值';
    }
  }
  
  switch (method) {
    case 'alipay': return '支付宝';
    case 'wechat': return '微信';
    case 'admin': return '后台充值';
    default: return '其他';
  }
}

function getAuditStatusText(status: number) {
  switch (status) {
    case 0: return '待审核';
    case 1: return '审核通过';
    case 2: return '审核拒绝';
    default: return '无需审核';
  }
}

function getAuditStatusType(status: number) {
  switch (status) {
    case 0: return 'warning';
    case 1: return 'success';
    case 2: return 'danger';
    default: return '';
  }
}

function openAuditDialog(row: any) {
  auditDialog.row = row;
  auditDialog.remark = '';
  auditDialog.visible = true;
}

function submitAudit(auditStatus: number) {
  if (!auditDialog.row) return;
  auditDialog.loading = true;
  RechargeAPI.auditBuy({
    id: auditDialog.row.id,
    auditStatus,
    remark: auditDialog.remark,
  })
    .then(() => {
      ElMessage.success('审核成功');
      auditDialog.visible = false;
      handleQuery();
    })
    .catch(() => {
      ElMessage.error('审核失败');
    })
    .finally(() => {
      auditDialog.loading = false;
    });
}

onMounted(() => {
  handleQuery();
});
</script>
